<?php

class PromotionController extends Zend_Controller_Action
{

    public function init()
    {
        $this->stylesBase = $this->view->baseUrl() . '/styles/';
    }
    
    public function indexAction()
    {         
        $view = $this->_getView('Promoties');  
        $promotions = $this->_getAllPromotions();  
        $view->promotions = $promotions;
        
       //ZendX_JQuery::enableView($view);
    }
    
    public function editAction()
    {   
        $view = $this->_getView('Edit promotion');
        $form = $this->_getEditPromotionForm();
        
        if ($this->getRequest()->isPost()) 
	{
            $formData  = $this->_request->getPost();
            if ($form->isValid($formData)) 
            {
                $view = $this->_getView('Fout - Toevoegen');  
                
                // Promotion toevoegen 
                $values = $form->getValues();

                $promotion_values['id'] = $values['promotion_id'];
                $promotion_values['title'] = $values['promotion_title'];
                $promotion_values['description'] = $values['promotion_description'];
                $promotion_values['content'] = $values['promotion_content'];
       
                $promotion = new Application_Model_Promotion($promotion_values);
                
                $promotionMapper = new Application_Model_PromotionMapper();
                $promotionMapper->save($promotion);

                return $this->_helper->redirector('index', 'promotion');                
            }
        }else{
            $id = (string)$this->getRequest()->getParam('id');
            
            $promotion = $this->_getViewPromotion($id);
            //$promotions->
            if(isset($promotion))
            {
                $form->populate($promotion->toArray());         
            }            
            $view->form = $form; 
        }   
    }

    /**
     * @param string $title
     * @return Zend_View
     */
    private function _getView($title)
    {
        $view = $this->view;
        $view->title = $title;
        $view->headTitle($view->title, 'prepend');                           // Zend_View_Helper_HeadTitle
        $view->headLink()->appendStylesheet($this->stylesBase . 'main.css'); // Zend_View_Helper_HeadLink

        return $view;
    }
    
    private function _getEditPromotionForm()
    {   
        return new Application_Form_EditPromotion(array(
            'action' => $this->view->baseUrl() . '/promotion/edit/',
            'post'   => 'post',
        ));
    }
    
    /**
     * @return Array
     */
    private function _getAllPromotions()
    {
        $promotionMapper = new Application_Model_PromotionMapper();
        $promotionArray = $promotionMapper->fetchAll();

        return $promotionArray;
    }
    
    private function _getViewPromotion($id)
    {    
        $promotionMapper = new Application_Model_PromotionMapper();
        $promotionArray = $promotionMapper->fetchById($id);
        
        return $promotionArray;
    }
}

